<?php
class Backend_Designer_Sub_Objects extends Backend_Designer_Sub
{
	/**
	 * Get list of project objects by object type
	 */
	public function listAction(){
		
		$this->_checkLoaded();
		$acceptedTypes = array('visual','stores');
		$type = Request::post('type', 'string', false);
		
		if(!in_array($type , $acceptedTypes , true))
			Response::jsonError($this->_lang->WRONG_REQUEST);

		switch ($type){
			case 'visual' :
				$list = $this->_fillContainers($this->_getProject()->getTree());
				Response::jsonArray($list);
				break;
		}	
	}
 	/**
     * Fill childs data array for tree panel
     * @param Tree $tree
     * @param mixed $root
     * @return array
     */
    protected function _fillContainers(Tree $tree , $root = 0)
    {
    	   $exceptions = array('Store' , 'Model');
           $result = array();   
           $childs = $tree->getChilds($root);      
           
           if(empty($childs))
                return array();
                   
           foreach($childs as $k=>$v)
           {
           		$object = $v['data'];
           		$objectClass = $object->getClass();
           		$objectName = $object->getName();
           		
           		if(in_array($objectClass , $exceptions , true))
           			continue;

           		$item = new stdClass();	
                $item->id = $v['id'];  
                $item->text = $objectName . ' ('.$objectClass.')';
                $item->expanded = true;
                $item->objClass = $objectClass;
                $item->leaf=false;
                $item->iconCls = 'objectIcon';   
                $item->allowDrag = true;
                
                if(!in_array($objectClass, Designer_Project::$containers , true)){
               		$item->leaf = true;
               		$item->iconCls = 'objectLeafIcon';
                }
                
                $item->children = array();
                   
                if($tree->hasChilds($v['id']))
                   $item->children = $this->_fillContainers($tree ,  $v['id']);
                                                                     
                $result[] = $item;
          }            
          return $result;     
    }
    /**
     * Sort Objects tree
     */
    public function sortAction()
    {
    	$this->_checkLoaded();
        $id = Request::post('id','string',false);
        $newParent = Request::post('newparent','string',false);
        if(!strlen($newParent))
        	$newParent = 0;
        $order = Request::post('order', 'array' , array());
        $project = $this->_getProject();   
        
        if(!$id  || !$project->objectExists($id))
            Response::jsonError($this->_lang->WRONG_REQUEST .' code1');
   
        $project->changeParent($id, $newParent);
        $count = 0;
        foreach ($order as $name) 
        {
        	if(!$project->setItemOrder($name, $count))
        		Response::jsonError($this->_lang->WRONG_REQUEST.' code2');
        	
        	$count ++;
        }
        $this->_storeProject();
        Response::jsonSuccess();
    }
}